package com.finance.rule.controller;

import com.finance.base.constant.BaseConstants;
import com.finance.base.exception.BaseException;
import com.finance.framework.controller.BaseController;
import com.finance.framework.controller.Result;
import com.finance.framework.lang.BeanUtils;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.rule.bean.pojo.MajorRuleLog;
import com.finance.rule.bean.pojo.WeightRuleLog;
import com.finance.rule.bean.pojo.PieceRuleLog;
import com.finance.rule.request.RuleLogReq;
import com.finance.rule.service.IRuleLogService;
import com.finance.system.bean.pojo.*;
import com.finance.system.request.LogisticsReq;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author: gyw
 * @Date: Created in 2017-09-26
 */
@Controller
@RequestMapping("/rule/rulelog")
public class RuleLogController extends BaseController {

    @Resource
    private IRuleLogService ruleLogService;

    @RequestMapping("/majorrule/index")
    public String dragIndex() {
        return "rule/rulelog/majorrulelog";
    }

    @RequestMapping("/weightrule/index")
    public String weightruleIndex() {
        return "rule/rulelog/weightrulelog";
    }

    @RequestMapping("/piecerule/index")
    public String pieceruleIndex() {
        return "rule/rulelog/piecerulelog";
    }

    @RequestMapping("/majorrule/getversionlist")
    public Result getMajorRuleVersionList() {
        List<String> versionList = ruleLogService.getMajorRuleVersionList();
        return getSuccessResult(versionList);
    }

    @RequestMapping("/majorrule/list")
    public Result majorRuleLogList(RuleLogReq form) {
        Page page = Page.getInstance(form);
        MajorRuleLog majorRuleLog = new MajorRuleLog();
        majorRuleLog.setVersion(form.getVersion());
        page.setParams(majorRuleLog);
        PageResultBO<MajorRuleLog> pageResultBO = ruleLogService.majorRuleLogListByPage(page);
        return getSuccessResult(pageResultBO);
    }


    @RequestMapping("/weightrule/getversionlist")
    public Result getWeightRuleVersionList() {
        List<String> versionList = ruleLogService.getWeightRuleVersionList();
        return getSuccessResult(versionList);
    }

    @RequestMapping("/weightrule/list")
    public Result weightRueLogList(RuleLogReq form) {
        Page page = Page.getInstance(form);
        WeightRuleLog weightRuleLog = new WeightRuleLog();
        weightRuleLog.setVersion(form.getVersion());
        page.setParams(weightRuleLog);
        PageResultBO<WeightRuleLog> pageResultBO = ruleLogService.weightRuleLogListByPage(page);
        return getSuccessResult(pageResultBO);

    }

    @RequestMapping("/piecerule/getversionlist")
    public Result getPieceRuleVersionList() {
        List<String> versionList = ruleLogService.getPieceRuleVersionList();
        return getSuccessResult(versionList);
    }

    @RequestMapping("/piecerule/list")
    public Result pieceRueLogList(RuleLogReq form) {
        Page page = Page.getInstance(form);
        PieceRuleLog pieceRuleLog = new PieceRuleLog();
        pieceRuleLog.setVersion(form.getVersion());
        page.setParams(pieceRuleLog);
        PageResultBO<PieceRuleLog> pageResultBO = ruleLogService.pieceRuleLogListByPage(page);
        return getSuccessResult(pageResultBO);

    }

    private Logistics convertBeanFromFrom(LogisticsReq logisticsReq) {
        Logistics logistics = new Logistics();
        Supplier supplier = new Supplier();
        supplier.setCode(logisticsReq.getSupplierCode());
        logistics.setSupplier(supplier);
        Organization acceptOrganzation = new Organization();
        acceptOrganzation.setOrgCode(logisticsReq.getOrgcodeAccept());
        Organization currentOrganzation = new Organization();
        currentOrganzation.setOrgCode(logisticsReq.getOrgcodeCurrent());
        Organization deliveryOrganzation = new Organization();
        deliveryOrganzation.setOrgCode(logisticsReq.getOrgcodeDelivery());
        Organization targetOrganzation = new Organization();
        targetOrganzation.setOrgCode(logisticsReq.getOrgcodeTarget());
        logistics.setOrgAccept(acceptOrganzation);
        logistics.setOrgCurrent(currentOrganzation);
        logistics.setOrgDelivery(deliveryOrganzation);
        logistics.setOrgTarget(targetOrganzation);
        try {
            BeanUtils.copyProperties(logistics, logisticsReq);
        } catch (Exception e) {
            throw new BaseException(BaseConstants.ERR_COPYBEAN);
        }
        return logistics;
    }
}
